const app = getApp();
import grace from "../../../utils/grace.js";

grace.page({
  /**
   * 页面的初始数据
   */
  data: {
    student_id: 0,
    accept_code: '未生成',
    button_text: '生成接送码'
  },
  onLoad(e) {
    wx.showLoading({
      title: '加载中...',
    })
    this.$data.student_id = e.student_id;
    // 查询孩子当日接送码
    let url = app.globalData.api + '/api/v1.0/student/security/' + e.student_id;
    this.$http.get(url, { token: wx.getStorageSync('token') }).then((res) => {
      if (res.data.code == 0) {
        this.$data.accept_code = res.data.data.code;
        this.$data.button_text = '重新生成接送码';
      }
      wx.hideLoading();
    }).catch(err => {
      console.log(err);
    })
  },
  formSubmit(e) {
    let that = this;

    let token = wx.getStorageSync('token');
    wx.showLoading({
      title: '加载中...',
    })
    let url = app.globalData.api + '/api/v1.0/security/' + wx.getStorageSync('partner_id') + '/' + that.$data.student_id;
    let params = {
      token: token,
      note: ''
    }
    that.$http.post(url, params).then((res) => {
      if (res.data.code == 0) {
        that.$data.accept_code = res.data.data;
        that.$data.button_text = '重新生成接送码';
      }else{
        wx.showModal({
          title: '错误',
          content: res.data.msg,
          showCancel: false
        })
      }
      wx.hideLoading();
    })
  }
})